package com.zhongying.sys.controller;

import java.util.List;

import org.ebaoquan.rop.thirdparty.org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zhongying.inteceptor.entity.RequestLogEntity;
import com.zhongying.inteceptor.util.RequestLogUtil;
import com.zhongying.sys.entity.SysUserEntity;
import com.zhongying.sys.service.SysUserService;
import com.zhongying.sys.utils.PageUtils;
import com.zhongying.sys.utils.R;

@Controller
@RequestMapping("sys/operate")
public class SysOperateLogController {
	
	@Autowired
	SysUserService sysUserService;
	
	@RequestMapping("init.html")
	public String page(@PathVariable("url") String url){
		return "sys/operate.html";
	}
	
	
	@RequestMapping("data")
	@ResponseBody
	public Object data(Integer page, Integer limit,String name,String url,String start,String end) {
		if (StringUtils.isNotBlank(name)) {
			SysUserEntity userEntity = sysUserService.queryByUserName(name);
			if (userEntity == null) {
				return R.ok();
			}
			Long userId = userEntity.getUserId();
			name = String.valueOf(userId);
		}
		
		Integer s = (page - 1) * limit;
		List<RequestLogEntity> list = RequestLogUtil.list(url, name, s, limit,start,end);
		for (RequestLogEntity requestLogEntity : list) {
			String operateUser = requestLogEntity.getOperateUser();
			if (StringUtils.isNotBlank(operateUser)) {
				long uid = Long.parseLong(operateUser);
				SysUserEntity entity = sysUserService.queryObject(uid);
				if (entity!=null) {
					requestLogEntity.setOperateUser(entity.getUsername());
				}
			}
		}
		// 查询总数
		Long total = RequestLogUtil.count(url, name,start,end);
		PageUtils pageUtil = new PageUtils(list, total.intValue(), limit, page);
		return R.ok().put("page", pageUtil);
	}
	
}
